#!/usr/bin/env bash

show_help() {
  cat<<USE
USAGE:
    rec [screencastname]
OPTIONS:
    --select|-s: use selection rectangle
    --public|-p: upload it to http://tyler.zone
    --time|-t: duration of recording (default: 10secs)
    --delay|-d: delay before recording starts (default: 5secs)
    --help|-h: show help and exit
EXAMPLE:
    rec --select --public whatchamajig
USE
}

notify() {
    local msg="$1"
    local level="${2:-normal}"

    if [[ "$level" == "normal" ]]; then
        printf "$(tput setaf 6)%s$(tput sgr0)\n" "$msg"
    fi

    if [[ "$level" == "error" ]]; then
        >&2 printf "$(tput setaf 1)%s$(tput sgr0)\n" "$msg"
    fi
}

record () {
    local i
    i=${delay:-5}

    notify "Recording starting in ${i} seconds"
    if [[ -n "$duration" ]]; then
        options="--duration=${duration}"
    fi

    options="${options} --delay=0 --x=${x_offset} --y=${y_offset} --width=${width} --height=${height}"

    # Countdown timer
    while (( i > 0 )); do
        notify "$i"
        sleep 1
        (( i-- ))
    done

    notify "GO!!!!"

    byzanz-record $options "$full_path"
}

get_full_width() {
    eval "$(xrandr -q | awk -F'[,x]' 'NR==1 {gsub("( |current)", ""); print "width="$3"\nheight="$4}')"
}

get_selection() {
    notify "Waiting for your selection..."
    eval "$(xrectsel | awk -F '[x+]' '{print "width=" $1 "\nheight=" $2 "\nx_offset=" $3 "\ny_offset=" $4}')"
}

take_recording() {
    if [[ -z "$select" ]]; then
        select=0
        x_offset=0
        y_offset=0
        get_full_width
    else
        get_selection
    fi

    if [[ -z "$path" ]]; then
        path="$(date -Is)"
    fi

    path="${path}.gif"
    full_path="${_SCREENCAST_PATH}/${path}"

    record

    notify "Recording available at $path"
}

make_public() {
    if ! command -v s3cmd > /dev/null 2>&1; then
        notify "[ERR] s3cmd command not found in $PATH" "critical"
    fi

    s3cmd put "${full_path}" "s3://tyler.zone/${path}"
    s3cmd setacl --acl-public "s3://tyler.zone/${path}"

    notify "[URL] http://tyler.zone/${path}"
    printf 'http://tyler.zone/%s' "${path}" | xsel -p
}

main() {
    if ! command -v xrectsel > /dev/null 2>&1; then
      notify "xrectsel command not found in \$PATH" "error"
    fi

    if ! command -v byzanz-record > /dev/null 2>&1; then
      notify "byzanz-record command not found in \$PATH" "error"
    fi

    mkdir -p "${_SCREENCAST_PATH:=$HOME/screencasts}"

    public=0

    while [ -n "$1" ]; do
        case "$1" in
            --select|-s)
                select=1
                ;;
            --time|-t)
                shift
                duration="$1"
                ;;
            --delay|-d)
                shift
                delay="$1"
                ;;
            --public|-p)
                public=1
                ;;
            --help|-h)
                show_help
                exit 0
                ;;
            *)
                path="$1"
                ;;
        esac
        shift
    done

  take_recording

  if (( public == 1 )); then
      make_public
  fi
}

main "$@"
